package com.pubg.information.controller;

import com.alibaba.fastjson.JSONObject;
import com.pubg.information.bean.Rotation;
import com.pubg.information.service.RotationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class RotationController {
    @Autowired
    RotationService rotationService;

    @RequestMapping("/main")
    public ModelAndView getInfo(){
        ModelAndView modelAndView = new ModelAndView("main");
        modelAndView.addAllObjects(rotationService.getRotation());
        return modelAndView;
    }

    @RequestMapping("/Release/submit")
    public ModelAndView submit(){
        ModelAndView modelAndView = new ModelAndView("showinfo");

        return modelAndView;
    }
    //按照类型查询
    @ResponseBody
    @RequestMapping("/Release/select")
    public List select(@RequestBody JSONObject json){
        System.out.println(json.toJSONString());
        List<Rotation> list = rotationService.getRotationByType(json.getString("type"));
        return list;
    }

    //按照id删除
    @ResponseBody
    @RequestMapping("/Release/delect")
    public List delect(@RequestBody JSONObject json){
        System.out.println(json.toJSONString());
        List<Rotation> list = rotationService.delectRotationById(json.getString("id"));
        return list;
    }

    //增加
    @ResponseBody
    @RequestMapping("/Release/insert")
    public Map insert(@RequestBody JSONObject json){
        System.out.println(json.toJSONString());
        Map map = rotationService.insRoation(json);
        return map;
    }
    //跳转咨询管理界面
    @RequestMapping("/Release")
    public  ModelAndView release(){
        ModelAndView modelAndView = new ModelAndView("showinfo");
        return modelAndView;
    }
    //跳转咨询管理界面
    @RequestMapping("/Info")
    public  ModelAndView insInfo(){
        ModelAndView modelAndView = new ModelAndView("insinfo");
        return modelAndView;
    }
    //跳转视频播放页面
    @RequestMapping("/PlayVideo/*")
    public  ModelAndView playVideo(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView("playvideo");
        String url = request.getRequestURI(); // 这个方法只能获得不包含参数的请求url，且只包含相对路径
        String[] urls =  url.split("/");
        modelAndView.addAllObjects(rotationService.selRoation(urls[urls.length-1]));
        return modelAndView;
    }
    //跳转资讯阅读页面
    @RequestMapping("/Read/*")
    public  ModelAndView read(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView("read");
        String url = request.getRequestURI(); // 这个方法只能获得不包含参数的请求url，且只包含相对路径
        String[] urls =  url.split("/");
        modelAndView.addAllObjects(rotationService.selRoation(urls[urls.length-1]));
        return modelAndView;
    }
}
